package com.qdxwx.manage.controller.advert;

import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.advert.AdvertService;
import com.qdxwx.data.service.advert.AdvertTypeService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.advert.Advert;
import com.qdxwx.models.advert.AdvertType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * banner图管理Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/advert")
public class AdvertController extends BaseController {

    @Autowired
    private AdvertService advertService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private AdvertTypeService advertTypeService;

    @GetMapping("/list.html")
    public String list(HttpServletRequest request, ModelMap mmap) {
        String channelId = request.getParameter("channelId");
        Channel channel = new Channel();
        channel.setIsDisabled(0);
        List<Channel> channelList = channelService.getList(channel,null);
        List<AdvertType> advertTypeList = advertTypeService.getList(new AdvertType(), null);
        mmap.put("advertTypeList", advertTypeList);
        mmap.put("channelId", TKString.isNotEmpty(channelId)?TKConvert.toInteger(channelId):1);
        mmap.put("channelList", channelList);
        return "advert/advert-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Advert advert = new Advert();
        advert.setTitle(TKString.queryKey(request.getParameter("title")));
        advert.setTypeId(TKConvert.toInteger(request.getParameter("typeId")));
        advert.setChannelId(TKConvert.toInteger(request.getParameter("channelId")));
        List<Advert> list = advertService.getList(advert, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(advertService.count(advert));
        return rspData;
    }

    /**
     * 新增banner图管理
     */
    @GetMapping("/add.html")
    public String add(HttpServletRequest request, ModelMap mmap) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        List<AdvertType> advertTypeList = advertTypeService.getList(new AdvertType(), null);
        Channel channel = new Channel();
        channel.setIsDisabled(0);
        List<Channel> channelList = channelService.getList(channel,null);
        mmap.put("channelList", channelList);
        mmap.put("advertTypeList", advertTypeList);
        mmap.put("channelId", channelId);
        return "advert/advert-add";
    }

    /**
     * 新增保存banner图管理
     */
    @Log(title = "banner图管理", businessType = BusinessType.INSERT)
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request) {
        String title = request.getParameter("title");
        if (TKString.isEmpty(title)) return error("请输入图片标题");
        String intro = request.getParameter("intro");
        if (TKString.isEmpty(intro)) return error("请输入图片描述");
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        if (typeId < 1) return error("请选择图片所属类型");
        String imageUrl = request.getParameter("imageUrl");
        if (TKString.isEmpty(imageUrl)) return error("请上传图片");
        Advert advert = new Advert();
        advert.setChannelId(TKConvert.toInteger(request.getParameter("channelId")));
        advert.setTitle(title);
        advert.setIntro(intro);
        advert.setTypeId(typeId);
        advert.setLinkUrl(request.getParameter("linkUrl"));
        advert.setSort(TKConvert.toInteger(request.getParameter("sort")));
        advert.setImageUrl(imageUrl);
        advert.setColor(request.getParameter("color"));
        return toAjax(advertService.insert(advert));
    }

    /**
     * 修改banner图管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, ModelMap mmap) {
        if(id < 1) return error404;
        Advert advert = advertService.get(id);
        if(advert == null) return error404;
        List<AdvertType> advertTypeList = advertTypeService.getList(new AdvertType(), null);
        Channel channel = new Channel();
        channel.setIsDisabled(0);
        List<Channel> channelList = channelService.getList(channel,null);
        mmap.put("channelList", channelList);
        mmap.put("advert", advert);
        mmap.put("advertTypeList", advertTypeList);
        return "advert/advert-edit";
    }

    /**
     * 修改保存banner图管理
     */
    @Log(title = "banner图管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error("缺少id参数");
        Advert advert = advertService.get(id);
        if (advert == null) return error("找不到指定参数");
        String title = request.getParameter("title");
        if (TKString.isEmpty(title)) return error("请输入图片标题");
        String intro = request.getParameter("intro");
        if (TKString.isEmpty(intro)) return error("请输入图片描述");
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        if (typeId < 1) return error("请选择图片所属类型");
        String imageUrl = request.getParameter("imageUrl");
        if (TKString.isEmpty(imageUrl)) return error("请上传图片");
        advert.setTitle(title);
        advert.setIntro(intro);
        advert.setTypeId(typeId);
        advert.setChannelId(TKConvert.toInteger(request.getParameter("channelId")));
        advert.setLinkUrl(request.getParameter("linkUrl"));
        advert.setSort(TKConvert.toInteger(request.getParameter("sort")));
        advert.setImageUrl(imageUrl);
        advert.setColor(request.getParameter("color"));
        return toAjax(advertService.update(advert));
    }

    /**
     * 删除banner图管理
     */
    @Log(title = "banner图管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if (id < 1) return error("缺少id参数");
        return toAjax(advertService.delete(id));
    }

    @PostMapping("/clean.json")
    @ResponseBody
    public AjaxResult clean(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        //清除Banner缓存
        CacheUtils.remove(CacheConstants.ADVERT_LIST +":banner:"+channelId);
        //清除Advert缓存
        CacheUtils.remove(CacheConstants.ADVERT_LIST +":advert:"+channelId);
        return success();
    }
}
